# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html.
# For more examples on how to use CMake, see https://github.com/android/ndk-samples.

# Sets the minimum CMake version required for this project.
cmake_minimum_required(VERSION 3.22.1)

# Declares the project name. The project name can be accessed via ${ PROJECT_NAME},
# Since this is the top level CMakeLists.txt, the project name is also accessible
# with ${CMAKE_PROJECT_NAME} (both CMake variables are in-sync within the top level
# build script scope).
project("jnitest" LANGUAGES CXX)
message(STATUS "CMAKE_SOURCE_DIR is set to: ${CMAKE_SOURCE_DIR}")

set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
set(JNI_LIBS_DIR /Users/ansion/AndroidStudioProjects/JniTest/app/src/main/jniLib)
#add_executable("jnitest" native-lib.cpp)
set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -std=c++17 -g -Wno-attributes -Wno-deprecated -Wno-deprecated-declarations -Wno-reorder -Wall -fexceptions -frtti -D__STDC_FORMAT_MACROS -fPIC")

include_directories(${JNI_LIBS_DIR}/include)
#link_directories(${JNI_LIBS_DIR}/arm64-v8a)
set(LIB_AVCODEC ${JNI_LIBS_DIR}/arm64-v8a/libavcodec.a)
set(LIB_AVFILTER ${JNI_LIBS_DIR}/arm64-v8a/libavfilter.a)
set(LIB_AVFORMAT ${JNI_LIBS_DIR}/arm64-v8a/libavformat.a)
set(LIB_AVUTIL ${JNI_LIBS_DIR}/arm64-v8a/libavutil.a)
set(LIB_SWRESAMPLE ${JNI_LIBS_DIR}/arm64-v8a/libswresample.a)
set(LIB_SWSCALE ${JNI_LIBS_DIR}/arm64-v8a/libswscale.a)

add_library(${CMAKE_PROJECT_NAME} SHARED
        # List C/C++ source files with relative paths to this CMakeLists.txt.
        native-lib.cpp
        util.cpp)


# Specifies libraries CMake should link to your target library. You
# can link libraries from various origins, such as libraries defined in this
# build script, prebuilt third-party libraries, or Android system libraries.
target_link_libraries(${CMAKE_PROJECT_NAME}
        # 使用链接器选项确保包含所有静态库的符号
        -Wl,--whole-archive
#        avfilter avformat avcodec avutil swscale swresample
        ${LIB_AVFILTER}
        ${LIB_AVFORMAT}
        ${LIB_AVCODEC}
        ${LIB_AVUTIL}
        ${LIB_SWSCALE}
        ${LIB_SWRESAMPLE}
        -Wl,--no-whole-archive
        # 链接其他必要的系统库
        -lm
        -lz
        -ldl
        android
        log)